{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f5fa29b-d97a-45df-9b0c-de01d55db607",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ace4da7c-9d76-41b6-b13f-c65905b5219e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.core.workflow import Context\n",
    "from llama_index.tools import QueryEngineTool, ToolMetadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38ce9a65-cb21-4b7a-bee8-fe9f0d60df81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# define query engine over paul graham's essay\n",
    "from llama_index import SimpleDirectoryReader, VectorStoreIndex\n",
    "import requests\n",
    "\n",
    "# download paul graham's essay\n",
    "response = requests.get(\n",
    "    \"https://www.dropbox.com/s/f6bmb19xdg0xedm/paul_graham_essay.txt?dl=1\"\n",
    ")\n",
    "essay_txt = response.text\n",
    "with open(\"pg_essay.txt\", \"w\") as fp:\n",
    "    fp.write(essay_txt)\n",
    "\n",
    "# load documents\n",
    "documents = SimpleDirectoryReader(input_files=[\"pg_essay.txt\"]).load_data()\n",
    "\n",
    "# build index\n",
    "index = VectorStoreIndex.from_documents(documents)\n",
    "\n",
    "# query engine\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8f2743d-ac08-486b-a5f7-290767f688e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# build query engine tool\n",
    "query_engine_tool = QueryEngineTool(\n",
    "    query_engine=query_engine,\n",
    "    metadata=ToolMetadata(\n",
    "        name=\"paul_graham\",\n",
    "        description=(\n",
    "            \"Provides a biography of Paul Graham, from childhood to college to adult\"\n",
    "            \" life\"\n",
    "        ),\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "628351bd-4055-49d6-9595-b77db168cb65",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_index.tools.text_to_image.base import TextToImageToolSpec\n",
    "from llama_index.llms import OpenAI\n",
    "\n",
    "llm = OpenAI(model=\"gpt-4\")\n",
    "\n",
    "text_to_image_spec = TextToImageToolSpec()\n",
    "tools = text_to_image_spec.to_tool_list()\n",
    "# Create the Agent with our tools\n",
    "agent = FunctionAgent(\n",
    "    tools=tools + [query_engine_tool], llm=llm\n",
    ")\n",
    "\n",
    "ctx = Context(agent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bbe2630-a0d6-484e-a75c-16bffded67dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\n",
    "    await agent.run(\n",
    "        \"generate an image of the car that Paul Graham bought after Yahoo bought his\"\n",
    "        \" company\",\n",
    "        ctx=ctx\n",
    "    )\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_hub",
   "language": "python",
   "name": "llama_hub"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
